var memberUtil=require('../../utils/memberUtil');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    serverImageUrl:app.globalData.baseUrl,
    addressShow:true,
    likeShow:true,
    dingdanShow:true,
    likes:[],
    region:[],
    isDefault:0,
    receiverProvince:"",
    recevierCity:"",
    recevierDistrictCounty:""

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    that.setData({
      userInfo:wx.getStorageSync("loginUserInfo").userInfo==undefined?"":wx.getStorageSync("loginUserInfo").userInfo,
    })
    if(that.data.userInfo.id==undefined) {
      memberUtil.getLogin(app,0,1,that,2);
    }

  },
  //调用地址
  adress:function(){
    var that=this;
    var json={memberId:this.data.userInfo.id}
    wx.request({
      url: app.globalData.baseUrl+'/member/receivingAddress/findReceivingAddressByMemberId',
      header: app.globalData.headerPost,
      method: 'POST',
      data:JSON.stringify(json),
      success:function(res){
        if(Number(res.data.length)>0 && res.statusCode==200) {
          wx.setNavigationBarTitle({
            title: '支付完成'
          })
          that.setData({
            addressShow: true,
            likeShow:false,
            dingdanShow:false
          })
        }else{
          wx.setNavigationBarTitle({
            title: '添加收货地址'
          })
          that.setData({
            addressShow: false,
            likeShow:true,
            dingdanShow:true
          })
        }
      }
    })
  },
  bindRegionChange: function (e) {
   var diqu=e.detail.value;
    var receiverProvince=diqu[0],recevierCity=diqu[1],recevierDistrictCounty=diqu[2];
    console.log(receiverProvince+"="+recevierCity+"="+recevierDistrictCounty);
    this.setData({
      receiverProvince:receiverProvince,
      recevierCity:recevierCity,
      recevierDistrictCounty:recevierDistrictCounty,
      region:diqu
    })
  },
  //你可能喜欢
  getLike: function (){
    var that=this;
    //获取地址
    wx.request({
      url: app.globalData.baseUrl+'/goods/mayBeLike',
      header: app.globalData.headerPost,
      method: 'GET',
      success:function(res){
        var likeObj="";
        if(res.data.length>0) {
          that.setData({
            likes: res.data
          })
        }

      }
    })
  },
  //页面跳转
  goodsDetailNavigate:function(e){
    wx.showLoading({
      title: '数据加载中',
      mask: true
    })
    var goodsId=e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/shop/shopDt?goodsId='+goodsId
    })
    wx.hideLoading();
  },
  switchFu: function (e) {
    var that=this;
    var value= e.detail.value;
    if(value){
      that.setData({
        isDefault: 0
      })
    }else{
      that.setData({
        isDefault:1
      })
    }
  },
  bindSave: function (e) {
    var that = this;
    var receiverName = e.detail.value.receiverName;
    if (receiverName == "" || receiverName ==null) {
      wx.showModal({
        title: '错误',
        content: '收货人不能为空',
        showCancel: false
      })
      return false;
    } else if (!(/^[a-zA-Z\u4e00-\u9fa5]+$/.test(receiverName))){
      wx.showModal({
        title: '错误',
        content: '收货人只能是汉字或字母',
        showCancel: false
      })
      return false;
    }

    var receiverMobilePhone = e.detail.value.receiverMobilePhone;
    if (receiverMobilePhone == "" || receiverMobilePhone == null) {
      wx.showModal({
        title: '错误',
        content: '手机号码不能为空',
        showCancel: false
      })
      return false;
    } else if (!(/^1[34578]\d{9}$/.test(receiverMobilePhone))){
      wx.showModal({
        title: '错误',
        content: '请输入正确的手机号码',
        showCancel: false
      })
      return false;
    }

    var receiverAddressDetail = e.detail.value.receiverAddressDetail;
    if (receiverAddressDetail == "" || receiverAddressDetail == null) {
      wx.showModal({
        title: '错误',
        content: '详细地址不能为空',
        showCancel: false
      })
      return false;
    }

    if(that.data.receiverProvince=="" || that.data.recevierCity=="" || that.data.recevierDistrictCounty==""){
      wx.showModal({
        title: '错误',
        content: '所在地区不能为空',
        showCancel: false
      })
      return false;
    }
    var isDefault = e.detail.value.isDefault;
    var requestJson={
      memberId:that.data.userInfo.id,
      receiverName: receiverName,
      receiverMobilePhone: receiverMobilePhone,
      receiverProvince:that.data.receiverProvince,
      recevierCity:that.data.recevierCity,
      recevierDistrictCounty:that.data.recevierDistrictCounty,
      receiverAddressDetail: receiverAddressDetail,
      receiverZipcode: "",
      isDefault: that.data.isDefault
    }
    console.log("requestJson="+JSON.stringify(requestJson))
    wx.request({
      url: app.globalData.baseUrl+'/member/receivingAddress/addReceivingAddress',
      header: app.globalData.headerPost,
      data:JSON.stringify(requestJson),
      method: 'POST',
      success:function(res){
        console.log("添加地址成功"+JSON.stringify(res.data));
        var likeObj="";
        if(res.data.status==200) {
          wx.showToast({
            title: '收货地址添加成功',
            icon: 'none',
            duration: 1000
          })
          wx.redirectTo({
            url: '/pages/my/myorder'
          })
        }

      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this;
    that.getLike();
    that.adress();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that=this;
    return {
      title: '我的二维码',
      path: "/pages/shop/shareSuccess?parentId=" + that.data.userInfo.id + "&enterType=0",
      imageUrl: app.globalData.loginUserInfo.erweimaUrl
    }
  }
})